package Thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: homechen
 * Date: 2024-03-15
 * Time: 22:49
 */
class ReentrantLockExmple {
    private ReentrantLock lock = new ReentrantLock();

    public void doDemo() {
        // 获取锁
        lock.lock();
        try {
            System.out.println("do something 1");
            lock.lock(); // 因为是可重入锁，可以再次获取锁
            try {
                System.out.println("do something 2");
            } finally {
                lock.unlock();
            }
        } finally {
            lock.unlock();
        }

    }
}

public class ReentrantLockDemo {
    public static void main(String[] args) {
        ReentrantLockExmple reentrantLockExmple = new ReentrantLockExmple();
        reentrantLockExmple.doDemo();
    }
}
